package senior.ioExer;

import java.io.File;
import java.util.HashSet;
import java.util.Iterator;

/**
 * 输入一个目录，输出其中所有的文件名
 *
 * @author shkstart
 * @creat 2022-04-15:07
 */
public class Exer6 {
    public static void main(String[] args) {
        File file = new File("D:\\Notepad++");
        HashSet<String> hashSet = new HashSet<>();
        findFile(file, hashSet);
        Iterator<String> iterator = hashSet.iterator();
        System.out.println(file.getName() + "有" + hashSet.size() + "个文件");
        System.out.println("---------------------");
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }


    }

    public static void findFile(File file, HashSet<String> hashSet) {
        if (!file.isDirectory()) {
            return;
        }
        String[] list = file.list();
        for (int i = 0; i < list.length; i++) {
            File dir = new File(file, list[i]);
            if (dir.isFile()) {
                hashSet.add(list[i]);
            } else {
                findFile(dir, hashSet);
            }
        }

    }
}
